package irio.pythcal;

import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;

import android.view.Display;
import android.view.ViewGroup.LayoutParams;
import android.widget.EditText;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Button;
import android.graphics.Color;
import android.view.View;
import android.widget.SeekBar;

public class PythCal extends Activity {
   /** Called when the activity is first created. */

	PaintView pView;
    Preview mPreview;

    Button btnAbout, btnTriangle, btnRectangle, btnSettings, btnHelp, btnCamera;
	TextView txtHeader;	
	SeekBar seekZoom;
	
	EditText edtScale;
	AlertDialog settingsDialog;
	AlertDialog scaleDialog;
	AlertDialog aboutDialog;
	AlertDialog helpDialog;
	
	static Container cont = new Container();
	
	
	
	private void setupGUI() {
		pView = new PaintView(this);
		
		btnAbout = new Button(this);
		btnHelp = new Button(this);
		btnTriangle = new Button(this);
		btnRectangle = new Button(this);
		btnSettings = new Button(this);
		btnCamera = new Button(this);
		txtHeader = new TextView(this);
		seekZoom = new SeekBar(this);
		
		seekZoom.setProgress(25);

		txtHeader.setText(cont.versionText);
		btnAbout.setText("About");
		btnHelp.setText("Help");
	    pView.setBackgroundColor(Color.WHITE);
	    btnTriangle.setText("Triangle");
	    btnRectangle.setText("Rectangle");
	    btnSettings.setText("Settings");
	    btnCamera.setText("Camera");

	    Display display = getWindowManager().getDefaultDisplay(); 
	    int width = display.getWidth();
	    int height = display.getHeight()/2;
	    
		LayoutParams lp = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
		
	    TableLayout otable = new TableLayout (this);
	    otable.setLayoutParams( new TableLayout.LayoutParams(1,2) );
	    {   
	    	TableLayout table = new TableLayout (this);
	    	table.setLayoutParams( new TableLayout.LayoutParams(3,3) );
	    	{
	    		TableRow tr = new TableRow(this);
	    		tr.addView(txtHeader);
	    		table.addView(tr);
	    		tr = new TableRow(this);
	    		tr.addView(btnAbout);
	    		tr.addView(btnSettings);
	    		tr.addView(btnHelp);
	    		//tr.addView(test, 100,40);
	    		table.addView(tr);
	    		tr = new TableRow(this);
	    		tr.addView(btnTriangle);
	    		tr.addView(btnRectangle);
	    		tr.addView(btnCamera);
	    		table.addView(tr);
	    	}
	    	
	    	TableRow tr = new TableRow(this);
	    	table.setStretchAllColumns(true);
	    	tr.addView(table,width,170);
	    	otable.addView(tr);
	     	tr = new TableRow(this);
	     	tr.addView(seekZoom);
	    	otable.addView(tr);
	     	tr = new TableRow(this);
	    	//tr.addView(pView);
	    	//tr.addView(mPreview);
	    	tr.addView(pView);
	    	otable.addView(tr);
	    	otable.setStretchAllColumns(true);
	    }
	       	
	    addContentView(otable, lp);
	    //addContentView(otable, lp);
	    pView.requestFocus();
	}
	
	@Override
	public void onPause()
	{
		super.onPause();
		pView.stop();
		
	}
	@Override
	public void onStop()
	{
		super.onPause();
		pView.stop();
		
	}
	@Override
	public void onRestart()
	{
		super.onPause();
		pView.stop();
		
	}


	
	@Override
	public void onResume()
	{
		super.onResume();
		pView.start();
		//if (pView.cameraEnabled)
		//	pView.start();
	}
	
	void setupListeners() {
		btnRectangle.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	pView.getShape().createShape(4);
            }
        });
		btnTriangle.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	pView.getShape().createShape(3);
            }
        });
		btnSettings.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	settingsDialog.show();
            }
        });
		btnAbout.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	aboutDialog.show();
            }
        });
		btnHelp.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	helpDialog.show();
            }
        });
		btnCamera.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	pView.cameraEnabled = !pView.cameraEnabled;

            	pView.setCamera();
            }
        });
        seekZoom.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener(){

     	   public void onProgressChanged(SeekBar seekBar, int progress,
     	     boolean fromUser) {
     		   cont.zoom = (progress-50)/50.0f;

     		   
     		   // TODO Auto-generated method stub
     	    //seekBarValue.setText(String.valueOf(progress));
     	   }
     	   public void onStartTrackingTouch(SeekBar seekBar) {
     	    // TODO Auto-generated method stub
     	   }

     	   public void onStopTrackingTouch(SeekBar seekBar) {
     	    // TODO Auto-generated method stub
     	   }
     });
		
	}

	
	@Override
	public void onCreate(Bundle savedInstanceState) {
	   super.onCreate(savedInstanceState);
	   setupGUI();
	   setupListeners();
	   
	   DialogFactory df = new DialogFactory(this, pView);
	   settingsDialog = df.buildSettingsDialog();
	   scaleDialog = df.buildScaleDialog();
	   aboutDialog = df.buildAboutDialog(cont.aboutText);
	   helpDialog = df.buildAboutDialog(cont.helpText);
	   
	}
}